package com.zhixinsou.wuyang.process;

import com.alibaba.fastjson.JSON;
import com.zhixinsou.wuyang.model.AnalysisReport;
import com.zhixinsou.wuyang.model.SheetModel;
import com.zhixinsou.wuyang.util.FileUtil;
import com.zhixinsou.wuyang.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessBenchmark {
    public static List<AnalysisReport> getAnalysisReportPageInfo(String filePath) {
        List<AnalysisReport> reports = new ArrayList<>();
        int startPage = 1;
        File file = new File(filePath);
        String[] lines = FileUtil.pdfReader(file, startPage, startPage).split("\r\n");
        Pattern pattern = Pattern.compile("([0-9]+)\\s-\\s([0-9]+)$");
        while (lines.length > 1 && lines[0].equals("Table of Contents")) {
            for (String line : lines) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    AnalysisReport report = new AnalysisReport();
                    report.setFilePath(filePath);
                    report.setFileName(file.getName());
                    report.setStartPage(Integer.parseInt(matcher.group(1)));
                    report.setEndPage(Integer.parseInt(matcher.group(2)));
                    report.setBrokerName("Benchmark");
                    
                    reports.add(report);
                }
            }

            startPage++;
            lines = FileUtil.pdfReader(filePath, startPage, startPage).split("\r\n");
        }
        return reports;
    }

    public static void getAnalysisReportDetail(AnalysisReport report) {
        Pattern patternVersion = Pattern.compile("Benchmark_([0-9]{4})_File\\s([0-9]+)[_]{0,1}");
        Matcher matcher = patternVersion.matcher(report.getFileName());
        if (matcher.find()) {
            int year = Integer.parseInt(matcher.group(1));
            int version = Integer.parseInt(matcher.group(2));
            System.out.println(year + " " + version);
            if(year < 2007)
                getAnalysisReportDetailVersion(report);
            else{
                if(year == 2007 && version < 5)
                    getAnalysisReportDetailVersion(report);
                else
                    getAnalysisReportDetailVersion2(report);
            }
        }
    }

    public static void getAnalysisReportDetailVersion(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
                break;
            }
        }

        //Analyst name, Company and Rating
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        Pattern patternName = Pattern.compile("(.*)●.*●.*@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if(StringUtil.isEmpty(line))
                continue;
            Matcher matcherEmail = patternEmail.matcher(line);
            Matcher matcherName = patternName.matcher(line);
            if (matcherEmail.find()) {
                if(matcherName.find()){
                    report.addAnalystName(matcherName.group(1));
                    report.setCompanyName(lines[i - 2]);
                }else{
                    String nameLine = lines[i - 2];
                    Pattern patternDate2 = Pattern.compile("(.*)((January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4})");
                    Matcher matcherDate2 = patternDate2.matcher(nameLine);
                    //Analyst name
                    if(matcherDate2.find()){
                        report.addAnalystName(matcherDate2.group(1));
                    }
                    else
                        report.addAnalystName(nameLine);
                    //Company
                    int j = i + 1;
                    while(StringUtil.isEmpty(lines[j].trim())){
                        j++;
                    }
                    String originLine = lines[j];
                    Pattern patternRating1 = Pattern.compile("(.*)Rating:\\s*([A-Za-z]+)");
                    Pattern patternRating2 = Pattern.compile("(.*)Company:\\s*([A-Za-z]+)");
                    Matcher matcherRating1 = patternRating1.matcher(originLine);
                    Matcher matcherRating2 = patternRating2.matcher(originLine);
                    if(matcherRating1.find()){
                        String originName = matcherRating1.group(1);
                        Pattern price = Pattern.compile("(.*)\\(\\$[0-9]+\\.[0-9]+\\)");
                        Matcher matcherPrice = price.matcher(originName);
                        if(matcherPrice.find()){
                            report.setCompanyName(matcherPrice.group(1));
                        }
                        else{
                            report.setCompanyName(originName);
                        }
                        report.setRating(matcherRating1.group(2));
                    }
                    else if(matcherRating2.find()){
                        String originName = matcherRating2.group(1);
                        Pattern price = Pattern.compile("(.*)\\(\\$[0-9]+\\.[0-9]+\\)");
                        Matcher matcherPrice = price.matcher(originName);
                        if(matcherPrice.find()){
                            report.setCompanyName(matcherPrice.group(1));
                        }
                        else{
                            report.setCompanyName(originName);
                        }
                        report.setRating(matcherRating2.group(2));
                    }
                }

            }
        }

        //Ticker
        if(report.getTicker() == null || report.getTicker().equals("")){
            Pattern patternTicker = Pattern.compile("Symbol\\s/\\sExchange(.*)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherTicker = patternTicker.matcher(line);
                if (matcherTicker.find()) {
                    String origin = matcherTicker.group(1);
                    System.out.println(String.format("The origin ticker line is %s", origin));
                    if(!origin.trim().equals(""))
                        report.setTicker(origin.trim().split("\\s")[0]);
                    break;
                }
            }
        }

        //处理混杂的情况
        if(report.getCompanyName() == null || report.getCompanyName().equals("")){

            patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");

            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate.matcher(line);
                if (matcherDate.find()) {
                    report.setDate(matcherDate.group());
                    int j = i + 1;
                    while(StringUtil.isEmpty(lines[j].trim())){
                        j++;
                    }
                    Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                    Matcher matcherCompany = patternCompany.matcher(lines[j]);
                    if(matcherCompany.find()){
                        report.setCompanyName(matcherCompany.group(1));
                        report.setTicker(matcherCompany.group(2));
                    }
                    break;
                }
            }
            if(report.getDate() == null || report.getDate().equals("")){
                Pattern patternDate2 = Pattern.compile("[0-9]+\\-[0-9]+\\-[0-9]+");
                for (int i = 0; i < lines.length; i++) {
                    String line = lines[i];
                    Matcher matcherDate = patternDate2.matcher(line);
                    if (matcherDate.find()) {
                        report.setDate(matcherDate.group());
                        int j = i + 1;
                        while(StringUtil.isEmpty(lines[j].trim())){
                            j++;
                        }
                        Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                        Matcher matcherCompany = patternCompany.matcher(lines[j]);
                        if(matcherCompany.find()){
                            report.setCompanyName(matcherCompany.group(1));
                            report.setTicker(matcherCompany.group(2));
                        }
                        break;
                    }
                }
            }

            //Ticker
            if(report.getTicker() == null || report.getTicker().equals("")){
                Pattern patternTicker = Pattern.compile("Ticker Symbol\\s*([A-Z]+)\\s");
                for (int i = 0; i < lines.length; i++) {
                    String line = lines[i];
                    Matcher matcherTicker = patternTicker.matcher(line);
                    if (matcherTicker.find()) {
                        report.setTicker(matcherTicker.group(1));
                        break;
                    }
                }
            }
            //Rating
            Pattern patternRating = Pattern.compile("Stock Rating\\s*([A-Za-z]+)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherRating = patternRating.matcher(line);
                if (matcherRating.find()) {
                    report.setRating(matcherRating.group(1));
                    break;
                }
            }
        }

        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
        lines = content.split("\r\n");
        Pattern patternSentence = Pattern.compile("([0-9]+)[\\.]?\\s+([^\r\n\\.]+\r\n)*[^\r\n\\.]*has[\r\n\\s]+a[\r\n\\s]+long[\r\n\\s]+position[\r\n\\s]+in[\r\n]{0,2}([^\r\n\\.]+[\r\n]{0,2})*(\\.\\s|\\.\r\n)?", Pattern.MULTILINE);
        Pattern patternTable = Pattern.compile("^Company\\s*Disclosure$");
        Matcher matcher = patternSentence.matcher(content);
        boolean tableStart = false;
        while (matcher.find()) {
            String companyIndex = matcher.group(1);
            String sentence = matcher.group().replaceAll("[\r\n]", " ");
            report.addOwnershipDisclosure(sentence);

            if (StringUtil.isEmpty(companyIndex)) {
                continue;
            }

            for (String line : lines) {
                Matcher matcherTable = patternTable.matcher(line.trim());
                if (!tableStart && matcherTable.find()) {
                    tableStart = true;
                    continue;
                }

                if (tableStart) {
                    Pattern pattern = Pattern.compile("([a-zA-Z]+[0-9]*).*" + companyIndex + "[^a-zA-Z0-9]+.*");
                    Matcher m = pattern.matcher(line);
                    if (m.find()) {
                        report.addCompaniesMentioned(m.group());
                    }

                    if(line.contains("Research Disclosure Legend") || StringUtil.isEmpty(line.trim()))
                        break;

                    Pattern patternEnd = Pattern.compile("^[0-9]+");
                    Matcher matcherEnd = patternEnd.matcher(line);
                    if (matcherEnd.find()) {
                        break;
                    }
                }
            }
        }

        if(report.getOwnershipDisclosures() == null || report.getOwnershipDisclosures().size() == 0){
            content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
            lines = content.split("\r\n");
            patternSentence = Pattern.compile("([^\r\n\\.]+\r\n)*[^\r\n\\.]*hold[s]?[\r\n\\s]+.*[\r\n\\s]+position[\r\n\\s]+in[\r\n]{0,2}([\r\n]{0,2}).*(\\s*\r\n){1}", Pattern.MULTILINE);
            matcher = patternSentence.matcher(content);
            if (matcher.find()) {
                String sentence = matcher.group().replaceAll("[\r\n]", " ");
                report.addOwnershipDisclosure(sentence);
            }
        }

        report.setPriorRecommendation("");
        report.setExchange("");
        report.setInitiatingCoverage("");
        System.out.println(JSON.toJSON(report));
    }

    public static void getAnalysisReportDetailVersion2(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        //Date
        Pattern patternDate = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                int j = i + 1;
                while(StringUtil.isEmpty(lines[j].trim())){
                    j++;
                }
                Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                Matcher matcherCompany = patternCompany.matcher(lines[j]);
                if(matcherCompany.find()){
                    report.setDate(matcherDate.group());
                    report.setCompanyName(matcherCompany.group(1));
                    report.setTicker(matcherCompany.group(2));
                }
                break;
            }
        }
        if(report.getDate() == null || report.getDate().equals("")){
            Pattern patternDate2 = Pattern.compile("(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]+\\S");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate2.matcher(line);
                if (matcherDate.find()) {
                    System.out.println(String.format("This is the second date pattern: %s", line));
                    String year = lines[i + 1].trim();
                    Pattern patternYear = Pattern.compile("[0-9]{4}");
                    Matcher matcherYear = patternYear.matcher(year);
                    if(matcherYear.find()){
                        int j = i + 2;
                        while(StringUtil.isEmpty(lines[j].trim())){
                            j++;
                        }
                        Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                        Matcher matcherCompany = patternCompany.matcher(lines[j]);
                        if(matcherCompany.find()){
                            report.setDate(matcherDate.group() + " " + year);
                            report.setCompanyName(matcherCompany.group(1));
                            report.setTicker(matcherCompany.group(2));
                        }
                    }
                    break;
                }
            }
        }

        if(report.getDate() == null || report.getDate().equals("")){
            Pattern patternDate2 = Pattern.compile("[0-9]{1,2}\\-[0-9]{1,2}\\-[0-9]{1,4}");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate2.matcher(line);
                if (matcherDate.find()) {
                    System.out.println(String.format("This is the third date pattern: %s", line));
                    int j = i + 1;
                    while(StringUtil.isEmpty(lines[j].trim())){
                        j++;
                    }
                    Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                    Matcher matcherCompany = patternCompany.matcher(lines[j]);
                    if(matcherCompany.find()){
                        report.setDate(matcherDate.group());
                        report.setCompanyName(matcherCompany.group(1));
                        report.setTicker(matcherCompany.group(2));
                    }
                    break;
                }
            }
        }
        if(report.getDate() == null || report.getDate().equals("")){
            Pattern patternDate2 = Pattern.compile("(Jan\\.|Feb\\.|Mar\\.|Apr\\.|May|Jun\\.|Jul\\.|Aug\\.|Sep\\.|Oct\\.|Nov\\.|Dec\\.){1}\\s*[0-9]+\\S\\s*[0-9]{4}");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate2.matcher(line);
                if (matcherDate.find()) {
                    System.out.println(String.format("This is the forth date pattern: %s", line));
                    int j = i + 1;
                    while(StringUtil.isEmpty(lines[j].trim())){
                        j++;
                    }
                    Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                    Matcher matcherCompany = patternCompany.matcher(lines[j]);
                    if(matcherCompany.find()){
                        report.setDate(matcherDate.group());
                        report.setCompanyName(matcherCompany.group(1));
                        report.setTicker(matcherCompany.group(2));
                    }
                    break;
                }
            }
        }

        if(report.getDate() == null || report.getDate().equals("")){
            Pattern patternDate2 = Pattern.compile("[0-9]{1,2}\\s*/\\s*[0-9]{1,2}\\s*/\\s*[0-9]{1,4}");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate2.matcher(line);
                if (matcherDate.find()) {
                    System.out.println(String.format("This is the fifth date pattern: %s", line));
                    int j = i + 1;
                    while(StringUtil.isEmpty(lines[j].trim())){
                        j++;
                    }
                    Pattern patternCompany = Pattern.compile("(.*)\\s*\\(([A-Z]+)\\)");
                    Matcher matcherCompany = patternCompany.matcher(lines[j]);
                    if(matcherCompany.find()){
                        report.setDate(matcherDate.group());
                        report.setCompanyName(matcherCompany.group(1));
                        report.setTicker(matcherCompany.group(2));
                    }
                    break;
                }
            }
        }

        if(report.getDate() == null || report.getDate().equals("")){
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherDate = patternDate.matcher(line);
                if (matcherDate.find()) {
                    report.setDate(matcherDate.group());
                    break;
                }
            }
        }

        //Ticker
        if(report.getTicker() == null || report.getTicker().equals("")){
            Pattern patternTicker = Pattern.compile("Ticker Symbol\\s*([A-Z]+)\\s");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherTicker = patternTicker.matcher(line);
                if (matcherTicker.find()) {
                    report.setTicker(matcherTicker.group(1));
                    break;
                }
            }
        }

        Pattern patternThree = Pattern.compile("(.*)\\(([A-Z]+)\\s*\\-\\s*(\\$[0-9]+\\.[0-9]+)\\S\\s([A-Za-z]+)\\)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherThree = patternThree.matcher(line);
            if (matcherThree.find()) {
                if(report.getCompanyName() == null || report.getCompanyName().equals(""))
                    report.setCompanyName(matcherThree.group(1));
                report.setTicker(matcherThree.group(2));
                report.setRating(matcherThree.group(4));
                break;
            }
        }

        Pattern patternThree2 = Pattern.compile("(.*)\\(([A-Z]+)\\S*\\s*([A-Za-z]+).*(\\$[0-9]+\\.[0-9]+)\\)");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            Matcher matcherThree = patternThree2.matcher(line);
            if (matcherThree.find()) {
                if(report.getCompanyName() == null || report.getCompanyName().equals(""))
                    report.setCompanyName(matcherThree.group(1));
                report.setTicker(matcherThree.group(2));
                report.setRating(matcherThree.group(3));
                break;
            }
        }

        //Rating
        if(report.getRating() == null || report.getRating().equals("")){
            Pattern patternRating = Pattern.compile("Stock Rating\\s*([A-Za-z]+)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherRating = patternRating.matcher(line);
                if (matcherRating.find()) {
                    report.setRating(matcherRating.group(1));
                    break;
                }
            }
        }
        if(report.getRating() == null || report.getRating().equals("")){
            Pattern patternRating = Pattern.compile("Rating\\s*([A-Za-z]+)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherRating = patternRating.matcher(line);
                if (matcherRating.find()) {
                    report.setRating(matcherRating.group(1));
                    break;
                }
            }
        }
        if(report.getRating() == null || report.getRating().equals("")){
            Pattern patternRating = Pattern.compile("Rating:\\s*([A-Za-z]+)");
            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];
                Matcher matcherRating = patternRating.matcher(line);
                if (matcherRating.find()) {
                    report.setRating(matcherRating.group(1));
                    break;
                }
            }
        }

        //Analyst name
        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(),false);
        lines = content.split("\r\n");
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        Pattern patternName = Pattern.compile("(.*) \u0003 .* \u0003 .*@[a-zA-Z0-9-]+\\.");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            if(StringUtil.isEmpty(line))
                continue;
            Matcher matcherEmail = patternEmail.matcher(line);
            Matcher matcherName = patternName.matcher(line);
            if (matcherEmail.find()) {
                if(matcherName.find()){
                    report.addAnalystName(matcherName.group(1));
                }
                else{
                    String nameLine = lines[i - 2];
                    report.addAnalystName(nameLine);
                }
            }
        }

        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
        lines = content.split("\r\n");
        Pattern patternSentence = Pattern.compile("([0-9]+)[\\.]?\\s+([^\r\n\\.]+\r\n)*[^\r\n\\.]*has[\r\n\\s]+a[\r\n\\s]+long[\r\n\\s]+position[\r\n\\s]+in[\r\n]{0,2}([^\r\n\\.]+[\r\n]{0,2})*(\\.\\s|\\.\r\n)?", Pattern.MULTILINE);
        Pattern patternTable = Pattern.compile("^Company\\s*Disclosure$");
        Matcher matcher = patternSentence.matcher(content);
        boolean tableStart = false;
        while (matcher.find()) {
            String companyIndex = matcher.group(1);
            String sentence = matcher.group().replaceAll("[\r\n]", " ");
            report.addOwnershipDisclosure(sentence);

            if (StringUtil.isEmpty(companyIndex)) {
                continue;
            }

            for (String line : lines) {
                Matcher matcherTable = patternTable.matcher(line.trim());
                if (!tableStart && matcherTable.find()) {
                    tableStart = true;
                    continue;
                }

                if (tableStart) {
                    Pattern pattern = Pattern.compile("([a-zA-Z]+[0-9]*).*" + companyIndex + "[^a-zA-Z0-9]+.*");
                    Matcher m = pattern.matcher(line);
                    if (m.find()) {
                        report.addCompaniesMentioned(m.group());
                    }

                    if(line.contains("Research Disclosure Legend") || StringUtil.isEmpty(line.trim()))
                        break;

                    Pattern patternEnd = Pattern.compile("^[0-9]+");
                    Matcher matcherEnd = patternEnd.matcher(line);
                    if (matcherEnd.find()) {
                        break;
                    }
                }
            }
        }

        if(report.getOwnershipDisclosures() == null || report.getOwnershipDisclosures().size() == 0){
            content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage(),false);
            lines = content.split("\r\n");
            patternSentence = Pattern.compile("([^\r\n\\.]+\r\n)*[^\r\n\\.]*hold[s]?[\r\n\\s]+.*[\r\n\\s]+position[\r\n\\s]+in[\r\n]{0,2}([\r\n]{0,2}).*(\\s*\r\n){1}", Pattern.MULTILINE);
            matcher = patternSentence.matcher(content);
            if (matcher.find()) {
                String sentence = matcher.group().replaceAll("[\r\n]", " ");
                String[] temps = sentence.split("\\s");
                String temp = temps[temps.length - 1];
                if(temp.equals("the")){
                    Pattern patternSentence2 = Pattern.compile("([^\r\n\\.]+\r\n)*[^\r\n\\.]*hold[s]?[\r\n\\s]+.*[\r\n\\s]+position[\r\n\\s]+in[\r\n]{0,2}([^\r\n\\.]+[\r\n]{0,2})*(\\.\\s|\\.\r\n)?", Pattern.MULTILINE);
                    matcher = patternSentence2.matcher(content);
                    if (matcher.find()) {
                        sentence = matcher.group().replaceAll("[\r\n]", " ");
                        report.addOwnershipDisclosure(sentence);
                    }
                }
                else
                    report.addOwnershipDisclosure(sentence);
            }
        }

        report.setPriorRecommendation("");
        report.setExchange("");
        report.setInitiatingCoverage("");
        System.out.println(JSON.toJSON(report));
    }

    public static void process(String rootDir, String resultPath) {
        List<AnalysisReport> reports = new ArrayList<>();
        File[] Files = new File(rootDir).listFiles();
        for (File file : Files) {
            if(file.getName().endsWith(".pdf"))
                reports.addAll(getAnalysisReportPageInfo(file.getPath()));
        }

        //以下代码为写入excel
        List<String> headers = new ArrayList<>();
        headers.add("Date");
        headers.add("Broker name");
        headers.add("Analyst name");
        headers.add("Company name");
        headers.add("Ticker");
        headers.add("Exchange");
        headers.add("Rating");
        headers.add("Initiating Coverage");
        headers.add("Prior recommendation");
        headers.add("Ownership disclosure");
        headers.add("Company Mentioned");
        headers.add("PDF file name");
        headers.add("StartPage");
        headers.add("EndPage");
        SheetModel sheetModel = FileUtil.makeSheet(resultPath, "result", headers);

        List<String[]> data = new ArrayList<>();
        for (AnalysisReport report : reports) {
            try {
                getAnalysisReportDetail(report);
            } catch (Exception e) {
                System.out.println(JSON.toJSON(report));
                e.printStackTrace();
                continue;
            }

            String[] line = new String[]{report.getDate(),
                    report.getBrokerName(),
                    JSON.toJSONString(report.getAnalystNames()),
                    report.getCompanyName(),
                    report.getTicker(),
                    report.getExchange(),
                    report.getRating(),
                    report.getInitiatingCoverage(),
                    report.getPriorRecommendation(),
                    JSON.toJSONString(report.getOwnershipDisclosures()),
                    JSON.toJSONString(report.getCompaniesMentioned()),
                    report.getFileName(),
                    report.getStartPage() + "",
                    report.getEndPage() + ""};
            data.add(line);
        }

        FileUtil.exportSheet(sheetModel.getSheet(), data);
        FileUtil.closeSheet(sheetModel);
        System.out.println("Success!");
    }
}
